﻿using Newtonsoft.Json;
using ThresholdAssistant.DataContract.Extensions;
using ThresholdAssistant.Enums;
using System;
using System.Text.Json.Serialization;

namespace ThresholdAssistant.DataContract.Result
{
    /// <summary>
    /// 返回结果契约基类
    /// </summary>
    [Serializable]
    public class MerchantContractResult
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public MerchantContractResult()
        {
            ErrorCode = ErrorTypeEnum.BusinessFailed.GetHashCode();
            ErrorMessage = ErrorTypeEnum.BusinessFailed.GetDescription();
        }

        /// <summary>
        /// 错误状态码
        /// </summary>
        [JsonProperty("nResult"), JsonPropertyName("nResult")]
        public int ErrorCode { get; set; }

        /// <summary>
        /// 错误信息，成功将返回空
        /// </summary>
        [JsonProperty("vcResult"), JsonPropertyName("vcResult")]
        public string ErrorMessage { get; set; }

        /// <summary>
        /// 业务流水号
        /// </summary>
        [JsonProperty("vcSerialNo"), JsonPropertyName("vcSerialNo")]
        public string SerialNo { get; set; } = Guid.NewGuid().ToString("N").ToUpper();

        /// <summary>
        /// 设置错误信息
        /// </summary>
        /// <param name="enumType">错误信息</param>
        public MerchantContractResult SetError<T>(T enumType) where T : Enum
        {
            ErrorCode = enumType.GetHashCode();
            ErrorMessage = enumType.GetDescription();
            return this;
        }

        /// <summary>
        /// 设置错误信息
        /// </summary>
        /// <param name="enumType">错误信息</param>
        /// <param name="serialNo">业务流水号</param>
        public MerchantContractResult SetError<T>(T enumType, string serialNo = "") where T : Enum
        {
            if (!string.IsNullOrWhiteSpace(serialNo))
            {
                SerialNo = serialNo;
            }
            ErrorMessage = enumType.GetDescription();
            ErrorCode = enumType.GetHashCode();
            return this;
        }

        /// <summary>
        /// 设置错误信息
        /// </summary>
        /// <param name="code">错误码</param>
        /// <param name="message">错误消息</param>
        /// <param name="serialNo"></param>
        public MerchantContractResult SetError(int code, string message, string serialNo = "")
        {
            ErrorCode = code;
            ErrorMessage = message;
            if (!string.IsNullOrWhiteSpace(serialNo))
            {
                SerialNo = serialNo;
            }
            return this;
        }

        /// <summary>
        /// 检查是否成功
        /// </summary>
        public bool IsSuceccful()
        {
            return ErrorCode == 1;
        }

        /// <summary>
        /// 设置成功返回信息
        /// </summary>
        public MerchantContractResult SetSuceccful(string serialNo = "")
        {
            if (!string.IsNullOrWhiteSpace(serialNo))
            {
                SerialNo = serialNo;
            }
            ErrorMessage = ErrorTypeEnum.SUCCESS.GetDescription();
            ErrorCode = ErrorTypeEnum.SUCCESS.GetHashCode();
            return this;
        }
    }

    /// <summary>
    /// 返回结果契约
    /// </summary>
    /// <typeparam name="T"></typeparam>
    [Serializable]
    public class MerchantContractResult<T> : MerchantContractResult
    {
        /// <summary>
        /// 数据
        /// </summary>
        [JsonProperty("Data"), JsonPropertyName("Data")]
        public T Data { get; set; }

        /// <summary>
        /// 设置成功返回信息
        /// </summary>
        /// <param name="data"></param>
        /// <param name="serialNo"></param>
        public MerchantContractResult<T> SetSuceccful(T data = default, string serialNo = "")
        {
            if (!string.IsNullOrWhiteSpace(serialNo))
            {
                SerialNo = serialNo;
            }
            Data = data;
            ErrorMessage = ErrorTypeEnum.SUCCESS.GetDescription();
            ErrorCode = ErrorTypeEnum.SUCCESS.GetHashCode();
            return this;
        }

        /// <summary>
        /// 设置错误信息
        /// </summary>
        /// <param name="enumType">错误信息</param>
        public new MerchantContractResult<T> SetError<E>(E enumType) where E : Enum
        {
            ErrorCode = enumType.GetHashCode();
            ErrorMessage = enumType.GetDescription();
            return this;
        }

        /// <summary>
        /// 设置错误信息
        /// </summary>
        /// <param name="enumType">错误信息</param>
        /// <param name="serialNo">业务流水号</param>
        public new MerchantContractResult<T> SetError<E>(E enumType, string serialNo = "") where E : Enum
        {
            if (!string.IsNullOrWhiteSpace(serialNo))
            {
                SerialNo = serialNo;
            }
            ErrorMessage = enumType.GetDescription();
            ErrorCode = enumType.GetHashCode();
            return this;
        }

        /// <summary>
        /// 设置错误信息
        /// </summary>
        /// <param name="code">错误码</param>
        /// <param name="message">错误消息</param>
        /// <param name="serialNo"></param>
        public new MerchantContractResult<T> SetError(int code, string message, string serialNo = "")
        {
            ErrorCode = code;
            ErrorMessage = message;
            if (!string.IsNullOrWhiteSpace(serialNo))
            {
                SerialNo = serialNo;
            }
            return this;
        }
    }
}